/*
 * Copyright 2007 - The JDPF Project Team (http://www.jdpf.org) 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package org.jdpf.core.kernel.plugins;

import java.util.List;

import org.jdpf.core.kernel.exceptions.CoreException;
import org.jdpf.core.kernel.exceptions.ErrorConverterException;
import org.jdpf.core.kernel.logging.ILogger;
import org.jdpf.core.kernel.logging.LoggerFactory;

public class DefaultConverter implements IConverter {

	// TODO javadocs & refactoring
	
	private static final ILogger logger = LoggerFactory.getDefault()
			.getLogger();

	private String transitionId;

	public DefaultConverter(String transitionId) {
		this.transitionId = transitionId;
	}

	public void validate(List<String> inputTypes, List<String> outputTypes)
			throws CoreException {
		int count = 0;
		boolean noInput = false;
		boolean noOutput = false;
		boolean anyInputs=false;
		for (String output : outputTypes) {
			if (output.equals("none")) {
				noOutput = true;
			}
			for (String input : inputTypes) {
				if(input.equals("any")){
					anyInputs=true;
				}else if (input.equals("none")) {
					noInput = true;
				}else if (output.equals(input)) {
					count++;
				}
				
			}
		}
		if (noInput&&noOutput) {
			// All ok
		} else if (noOutput && !noInput) {
			// Error
			throw new ErrorConverterException("The previous block has no output data "
					+ "while this block need input data to work properly", transitionId); 
		} else if (!anyInputs&&count == 0) {
			// Error
			throw new ErrorConverterException("Output data types"
					+ " from the previous block are not compatible "
					+ "with input data types of this block", transitionId);
		} else if (!anyInputs&&count < outputTypes.size()) {
			// Warning
			logger.log("Some Output data types from "
					+ "the previous block are not compatible with input "
					+ "data types of this block. Some problems may occur"); 
		}else if(anyInputs&&noOutput){
			throw new ErrorConverterException("Output data types"
					+ " from the previous block are not compatible "
					+ "with input data types of this block", transitionId);
		}
	}

}
